<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改任务')"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-task-edit" th:object="${task}">
        <input name="id" th:field="*{id}" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label">任务名称：</label>
            <div class="col-sm-8">
                <input name="name" id="taskName" th:field="*{name}" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">平台类型：</label>
            <div class="col-sm-8">
                <select name="platformId" class="form-control m-b" th:with="type=${platformTypes}">
                    <option th:each="dict : ${type}" th:text="${dict.name}" th:value="${dict.id}"
                            th:field="*{platformId}"></option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">任务类型：</label>
            <div class="col-sm-8">
                <select name="type" class="form-control m-b" th:with="type=${@dict.getType('sys_task_type')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"
                            th:field="*{type}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">状态：</label>
            <div class="col-sm-8">
                <select name="state" class="form-control m-b" th:with="type=${@dict.getType('sys_publish_state')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"
                            th:field="*{state}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">金额：</label>
            <div class="col-sm-8">
                <input name="amount" id="amount" onkeyup="calculate()" th:field="*{amount}" class="form-control"
                       type="text">
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">数量：</label>
            <div class="col-sm-8">
                <input name="num" id="num" onkeyup="calculate()" th:field="*{num}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">总价：</label>
            <div class="col-sm-8">
                <input name="total" id="total" onkeyup="calculate()" th:field="*{total}" class="form-control"
                       type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">视频链接：</label>
            <div class="col-sm-8">
                <textarea name="videoUrl" class="form-control" required>[[${task.videoUrl}]]</textarea>

            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">视频id：</label>
            <div class="col-sm-8">
                <textarea name="videoId" class="form-control" type="">[[${task.videoId}]]</textarea>

            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var prefix = ctx + "task/task";
    $("#form-task-edit").validate({
        onkeyup: false,
        rules:{
            taskName:{
                remote: {
                    url: prefix + "/checkTaskNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "taskName": function() {
                            return $.common.trim($("#taskName").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
        },
        messages: {
            "taskName":{
                remote: "任务名称已经存在"
            }
        },
        focusCleanup: true
    });

    //计算金额
    function calculate() {
        let num = $("#num").val();
        let amount = $("#amount").val();
        let total = $("#total").val();
        if (num !== undefined && amount !== undefined && num !== '' && amount !== '') {
            total = parseFloat(num) * parseFloat(amount);
            $("#total").val(total)
        }
    }

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/edit", $('#form-task-edit').serialize());
        }
    }
</script>
</body>
</html>